package tree;

public class BuildTree {
    static int i = 0;// 计数
    public static TreeNode buildTree(int[] list) {
        if (list == null || list.length == 0) {
            return null;
        }
        TreeNode node = new TreeNode();
        // 如果二叉树数据空值正确，此判断可以省略
        node.setValue(list[i++]);
        if (node.getValue() == 0) {
            return null;
        } else {
            node.setLeft(buildTree(list));
            node.setRight(buildTree(list));
        }
        return node;
    }

    public static void main(String[] args) {
        int[] list = new int[]{1, 2, 0, 3, 4, 0, 0, 0, 5, 6, 0, 0, 7, 8, 9, 0, 0, 0, 0};

        TreeNode root = buildTree(list);
        PreTraverse.preTraverse(root);
    }

}
